﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HPIT.Web
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void btn_Submit_Click(object sender, EventArgs e)
        {
            //1. 收集用户输入的信息
            string userName = txt_UserName.Value.Trim(); //用户名
            string password = txt_Password.Value.Trim();//密码

            //2. 准备连接字符串和SQL语句
            //2.1 声明数据库连接字符串
            string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;

            //2.2 声明SQL语句
            string sql = @"SELECT * FROM dbo.UserInfo WHERE UserName =@UserName AND [Password]=@Password";

            //2.3 定义参数
            SqlParameter[] pams = {
                new SqlParameter("@UserName",userName),
                new SqlParameter("@Password",password)
            };

            using (SqlDataReader reader = DbHelper.ExecuteReader(sql,pams))
            {
                //6.1 如果有行数据，说明用户名和密码匹配，登录成功
                if (reader.HasRows)
                {
                    //6.2 使游标向下移动一行
                    if (reader.Read())
                    {
                        //6.3 根据索引获取数据
                        string loginUserName = reader["UserName"].ToString();//获取登录用户名
                        string email = reader["Email"].ToString();//获取邮箱
                        Response.Write($"{loginUserName}欢迎您，您的邮箱是：{email}");
                        Response.End();
                    }
                }
                //6.2 否则用户名和密码不匹配
                else
                {
                    lbl_Msg.Text = "用户名或密码有误";
                }
            }


           

        }
    }
}